package itsm.isperp.module.controller.problem;

import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppUser;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.customer.CusCustomerService;
import itsm.isperp.module.service.problem.ProbProblemService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.utils.ProjectDomainUtils;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("problem/search")
public class ProbProblemSearchController {

	@Autowired
	protected ProbProblemService probProblemService;

	@Autowired
	protected AppDictService appDictService;

	@Autowired
	protected ProjProjectService projProjectService;

	@Autowired
	protected CusCustomerService cusCustomerService;

	@Autowired
	protected AppUserService appUserService;

	/**
	 * 搜索页面
	 * 
	 * @return
	 */
	@RequestMapping(value = { "manage", "manageView", "" })
	public ModelAndView manage(String projectId) {
		ModelAndView mv = new ModelAndView("problem/search/manage");
		// 登记者
		mv.addObject("projectId", projectId);

		// 初始化筛选条件
		// 项目
		mv.addObject(
				"projects",
				projProjectService.findAll(
						new JqGridRequest().isPageable(false).setSelectColumns(
								"id", "name")).getContent());

		List<AppUser> appUser = appUserService.findAll(
				new JqGridRequest()
						.isPageable(false)
						.setSelectColumns("name", "fullname")
						.addFilterRule("fullname",
								JqGridFilterRuleOp.isnotnull, null))
				.getContent();
		// 请求人
		mv.addObject("requesters", appUser);
		// 实施者
		mv.addObject("executers", appUser);

		// 优先级
		mv.addObject("var_priority",
				appDictService.findByType("PROBLEM_PRIORITY"));

		// 关闭码
		// mv.addObject("var_closeCode",
		// appDictService.findByType("CHANGE_CLOSECODE"));

		// 状态
		mv.addObject("problem_status",
				appDictService.findByType("PROBLEM_STATUS"));
		
		int dataRange=ProjectDomainUtils.getUserMaxDataRange();
    mv.addObject("dataRange",dataRange);

		return mv;
	}

	/**
	 * 根据分页器获取实体列表的json格式数据
	 * 
	 * @param request
	 *            {@link JqGridRequest} jqGrid数据请求（查询条件、排序）
	 * @param req
	 *            {@link HttpServletRequest} http请求
	 * @return json格式数据
	 */
	@RequestMapping("list")
	@ResponseBody
	public PageResponse<Map<String, Object>> list(JqGridRequest request,
			HttpServletRequest req) {

		String projectId = req.getParameter("projectId");
		if (StringUtils.isNotEmpty(projectId)) {
			request.addFilterRule("projectId", JqGridFilterRuleOp.eq, projectId);
		}
		return this.probProblemService.searchAllByFilter(request).toPage();

	}
}
